<!--
 * @Author: 灰佳小白 1150248441@qq.com
 * @Date: 2022-10-13 15:32:25
 * @LastEditors: lxy 2110251760@qq.com
 * @LastEditTime: 2022-10-18 15:39:42
 * @FilePath: \家政项目平台\jep-training-trinity-group\briup-ui\src\pages\admin\info\article\Publish.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <!-- 发布资讯页面 -->
  <div>

    <!-- {{userId}} -->
    <!-- {{form}} -->
    <el-form ref="myform" :model="form" label-width="80px" :rules="rules">
      <el-form-item label="标题" prop="title">
        <el-input v-model.trim="form.title" placeholder="请输入文章标题" style="width: 1000px;" />
      </el-form-item>
      <el-form-item label="所属分类" prop="categoryId">
        <el-select v-model="form.categoryId" placeholder="请选择所属栏目">
          <el-option v-for="item in categortData" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="封面" prop="cover">
        <el-upload class="avatar-uploader" :action="uploadFileURL" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
      </el-form-item>
      <!-- 富文本编辑器 -->
      <el-form-item label="正文" prop="content">
        <Tinymce ref="container" v-model="form.content" :height="200" />
      </el-form-item>
      <el-form-item style="float: right;">
        <el-button @click="resetForm('myform')">返回</el-button>
        <el-button type="primary" @click="submitForm('myform')">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import { post } from '@/utils/request'
import { findCategory } from '@/api/infoCategory'
import { uploadFileURL } from '@/utils/config'
import { mapGetters } from 'vuex'
export default {
  components: {
    Tinymce
  },
  data() {
    return {
      uploadFileURL,
      imageUrl: '',
      form: {
        title: '',
        categoryId: '',
        content: '',
        authorId: '',
        cover: ''
      },
      categortData: [],
      rules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        categoryId: [{ required: true, message: '请选择所属栏目', trigger: 'change' }],
        content: [{ required: true, message: '请输入正文的描述', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 图片上传成功回调
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.form.cover = res.data
    },
    // 图片上传之前大小判断
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    async getCategory() {
      const res = await findCategory()
      // console.log(res)
      this.categortData = res.data
    },
    submitForm() {
      this.form.authorId = this.userId
      console.log(this.form)
      this.$refs.myform.validate(valid => {
        if (valid) {
          this.$confirm('是否确认发布该资讯?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(async() => {
              const res = await post('/article/saveOrUpdate', this.form)
              this.$message({
                type: 'success',
                message: res.message
              })
              this.$router.go(-1)
            })
            .catch(() => {
              this.$message({
                type: 'info',
                message: '已取消发布'
              })
            })
        } else {
          // console.log('error submit!!')
          return false
        }
      })
    },
    resetForm() {
      this.$router.go(-1)
    }
  },
  computed: {
    ...mapGetters(['userId'])
  },
  created() {
    this.getCategory()
  },
  mounted() {}
}
</script>
<style scoped>
</style>
